package cn.iocoder.yudao.module.booking.controller.admin.instructorinfo;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.booking.controller.admin.instructorinfo.vo.InstructorInfoPageReqVO;
import cn.iocoder.yudao.module.booking.controller.admin.instructorinfo.vo.InstructorInfoRespVO;
import cn.iocoder.yudao.module.booking.controller.admin.instructorinfo.vo.InstructorInfoSaveReqVO;
import cn.iocoder.yudao.module.booking.dal.dataobject.instructorinfo.InstructorInfoDO;
import cn.iocoder.yudao.module.booking.service.instructorinfo.InstructorInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 教练信息")
@RestController
@RequestMapping("/booking/instructor-info")
@Validated
public class InstructorInfoController {

    @Resource
    private InstructorInfoService instructorInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建教练信息")
    @PreAuthorize("@ss.hasPermission('booking:instructor-info:create')")
    public CommonResult<Long> createInstructorInfo(@Valid @RequestBody InstructorInfoSaveReqVO createReqVO) {
//        Long tenantId = TenantContextHolder.getTenantId();
//        System.out.println(tenantId);
        return success(instructorInfoService.createInstructorInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教练信息")
    @PreAuthorize("@ss.hasPermission('booking:instructor-info:update')")
    public CommonResult<Boolean> updateInstructorInfo(@Valid @RequestBody InstructorInfoSaveReqVO updateReqVO) {
        instructorInfoService.updateInstructorInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('booking:instructor-info:delete')")
    public CommonResult<Boolean> deleteInstructorInfo(@RequestParam("id") Long id) {
        instructorInfoService.deleteInstructorInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教练信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('booking:instructor-info:query')")
    public CommonResult<InstructorInfoRespVO> getInstructorInfo(@RequestParam("id") Long id) {
        InstructorInfoDO instructorInfo = instructorInfoService.getInstructorInfo(id);
        return success(BeanUtils.toBean(instructorInfo, InstructorInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练信息分页")
    @PreAuthorize("@ss.hasPermission('booking:instructor-info:query')")
    public CommonResult<PageResult<InstructorInfoRespVO>> getInstructorInfoPage(@Valid InstructorInfoPageReqVO pageReqVO) {
        PageResult<InstructorInfoDO> pageResult = instructorInfoService.getInstructorInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InstructorInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练信息 Excel")
    @PreAuthorize("@ss.hasPermission('booking:instructor-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInstructorInfoExcel(@Valid InstructorInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InstructorInfoDO> list = instructorInfoService.getInstructorInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教练信息.xls", "数据", InstructorInfoRespVO.class,
                        BeanUtils.toBean(list, InstructorInfoRespVO.class));
    }

}